/*
 *
 *  Copyright (C) 2012-2024
 *  All rights reserved, Designed By 杭州连线客信息技术有限公司
 *  注意：
 *  本软件为杭州连线客信息技术有限公司开发研制，未经购买不得使用
 *  购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 *  一经发现盗用、分享等行为，将追究法律责任，后果自负
 *
 */

package com.lxk.oms.flow.send;

import cn.hutool.core.util.StrUtil;
import com.lxk.event.helper.EventHelper;
import com.lxk.event.oms.OrderOptionLogEvent;
import com.lxk.event.oms.enums.OrderEnums;
import com.lxk.oms.order.domain.OmsOrder;
import com.lxk.oms.order.domain.vo.OmsOrderVo;
import com.lxk.oms.order.service.IOmsOrderService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.log.LFLog;
import com.yomahub.liteflow.log.LFLoggerManager;
import lombok.AllArgsConstructor;
import org.dromara.common.core.exception.ServiceException;

import java.util.Date;

import static com.lxk.oms.flow.send.FlowSendOrderNodeConst.KD_SEND_ORDER_NODE;

/**
 * 快递
 *
 * @author heyi
 * 2024/9/3
 */
@AllArgsConstructor
@LiteflowComponent(KD_SEND_ORDER_NODE)
public class OrderKdSendOrderNode extends NodeComponent {
    private final LFLog log = LFLoggerManager.getLogger(FlowExecutor.class);
    private final IOmsOrderService omsOrderService;


    @Override
    public void process() throws Exception {
        SendOrderContext contextBean = getContextBean(SendOrderContext.class);
        Long orderId = contextBean.getOrderId();
        OmsOrderVo order = contextBean.getOmsOrderVo();
        OmsOrder update = new OmsOrder();
        update.setDeliveryName(contextBean.getDeliveryName());
        update.setDeliverySn(contextBean.getDeliverySn());
        update.setShipperCode(contextBean.getShipperCode());
        update.setDeliveryTime(new Date());
        // 更新提货状态
        Boolean success = omsOrderService.updateOrderStatusWithInfo(orderId,
            OrderEnums.OrderStatus.SEND_300,
            OrderEnums.OrderStatus.WAIT_CONFIRM_350,
            update
        );
        if (!success) {
            OmsOrderVo omsOrderVo = omsOrderService.queryById(orderId);
            Integer status = omsOrderVo.getStatus();
            String msg = StrUtil.format("确认发货时,订单{}尚未处于待发货,订单状态:{}", order.getOrderNo(), OrderEnums.OrderStatus.getByCode(status).getName());
            log.error(msg);
            throw new ServiceException(msg);
        }
    }

    @Override
    public void onSuccess() throws Exception {
        SendOrderContext contextBean = getContextBean(SendOrderContext.class);
        if (contextBean.getSendOptionEvent()) {
            OrderOptionLogEvent send = new OrderOptionLogEvent();
            OrderEnums.DeliveryType deliveryType = contextBean.getDeliveryType();
            OmsOrderVo omsOrder = contextBean.getOmsOrderVo();
            send.setUserId(omsOrder.getUserId());
            send.setOrderSn(omsOrder.getOrderNo());
            send.setTenantId(contextBean.getTenantId());
            send.setOptionUserId(contextBean.getOptionUserId());
            send.setOptionUserType(contextBean.getOptionUserType());
            send.setOrderLogEnums(OrderOptionLogEvent.OrderLogEnums.ORDER);
            send.setContent(OrderOptionLogEvent.OrderContentFormatEnums.KD_SEND_ORDER.
                formatContent(
                    deliveryType.getName(),
                    contextBean.getDeliveryName(),
                    contextBean.getDeliverySn()

                ));
            EventHelper.publishEvent(send);
        }

    }
}
